<template>
  <el-checkbox-group :disabled="isReadOnly" v-show="isShow" @change="onItemClick" v-model="value" :name="field.vModel">
    <el-checkbox v-for="(item,  index) in data" :label="item[valueName]" :key="index">{{item[textName]}}</el-checkbox>
  </el-checkbox-group>
</template>

<script>
  export default {
    name: 'f2bpmuiCheckbox',
    props: ['field', 'fieldvalue','readOnly'],
    data: function () {
      return {
        isView: false,
        isShow: true,
        isReadOnly: false,
        inputCtrlOption: null,
        textName: 'text',
        valueName: 'value',
        value: [],
        sqlkeyArray: [{
          dataField: 'data',
          sqlkey: '',
          parm: null,
          flag: null,
          change: null
        }],
        configTypeArray: [{
          dataField: 'data',
          configType: '',
          flag: ''
        }
        ],
        type: '',
        data: []
      }
    },
    watch: {
      field: {
        handler(n, o) {
          this.initail()
        },
        deep: true,
        immediate: false
      }
    },
    created: function () {
      var that = this;
      that.inputCtrlOption = that.field.inputCtrlOption;
      if (typeof that.fieldvalue === 'string' && that.fieldvalue != undefined && that.fieldvalue != null && that.fieldvalue != '') {
        that.value = that.fieldvalue.indexOf("[") > -1 ? eval('(' + that.fieldvalue + ')') : that.fieldvalue.split(',');
      } else {
        that.value = that.fieldvalue || [];
      }
      if (that.readOnly!=undefined&&that.readOnly!=null) {
        that.isReadOnly = that.readOnly;
      }
      that.initail();
    }, mounted() {
      var that = this;
    },
    methods: {
      initail() {
        var that = this;
        if (WFContext.isDesign == true || that.isShow == false) {
          return false;
        }
        if (that.field.inputCtrlOption) {
          var json = typeof that.field.inputCtrlOption === 'object' ? that.field.inputCtrlOption : eval('(' + that.field.inputCtrlOption + ')')
          if (json.type == 'custom') {
            that.type = json.type;
            that.data = json.data;
            that.textName = 'text';
          } else if (json.type == 'dic') {
            that.type = json.type;
            var configType = json.data;
            that.configTypeArray[0].configType = configType;
            FUI.Form.initailConfigTypeComboBox(that);
          } else if (json.type == 'sql') {
            that.type = json.type;
            var sql = json.data;
            var data = Object.toAjaxJson('/workflow/fuiFramework/', 'getTextValueDataJsonBySql', { sql: sql });
            that.data = data;
          } else if (json.type == 'dataService') {
            var code = json.data.code;
            var isMulti = json.data.isMulti;
            var bindFields = json.data.data;
            // 高级参数格式：[{'key':'A','value':'1','orderNum':1},{'key':'B','value':'Items2','orderNum':2}]
            var parmsArr = json.data.parms || [];
            if (parmsArr && typeof parmsArr === 'string') {
              parmsArr = eval('(' + parmsArr + ')');
            }
            var parms = {};
            if (parmsArr && parmsArr.length > 0) {
              $.each(parmsArr, function (i, item) {
                parms[item.key] = item.value;
              })
            }
            var res = FUI.Sys.getServiceData(code, parms);
            var textValueData = [];
            var data = [];
            if (res.success != undefined && res.success == true) {
              data = res.data;
            } else if (res.success == undefined) {
              data = res;
            }
            if (data && typeof data === 'string') {
              data = eval('(' + data + ')');
            }
            textValueData = data;
            // 默认认为你是存丰text,value属性的，
            // 如果存在绑定字段的配置,那么就按配置的字段钻取
            if (bindFields && bindFields.length > 0) {
              textValueData = [];
              var textProName = bindFields.find(a => a.field == 'text').property;
              var valueProName = bindFields.find(a => a.field == 'value').property;
              $.each(data, function (_, row) {
                var t = row[textProName];
                var v = row[valueProName];
                textValueData.push({ 'text': t, 'value': v })
              })
            }
            that.data = textValueData;
          }
        }
      },
      onItemClick(value) {
        var that = this
        if (value != undefined) {
          this.$emit('updatevalue', that.field.fieldName, value);
        }
      }
    }
  }
</script>